window.onload = function(){
    layui.use(['jquery','layer'],function(){
        var $ = layui.jquery,layer=layui.layer;

        $(function () {
            query();
        })

        var zTreeNodes;
        var setting = {
            view: {
                showLine: false,
                showIcon: false,
                addDiyDom: addDiyDom
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };
        /**
         * 自定义DOM节点
         */
        function addDiyDom(treeId, treeNode) {
            var spaceWidth = 15;
            var liObj = $("#" + treeNode.tId);
            var aObj = $("#" + treeNode.tId + "_a");
            var switchObj = $("#" + treeNode.tId + "_switch");
            var icoObj = $("#" + treeNode.tId + "_ico");
            var spanObj = $("#" + treeNode.tId + "_span");
            aObj.attr('title', '');
            aObj.append('<div class="diy swich"></div>');
            var div = $(liObj).find('div').eq(0);
            switchObj.remove();
            spanObj.remove();
            icoObj.remove();
            div.append(switchObj);
            div.append(spanObj);
            var spaceStr = "<span style='height:1px;display: inline-block;width:" + (spaceWidth * treeNode.level) + "px'></span>";
            switchObj.before(spaceStr);
            var editStr = '';
            editStr += '<div class="diy">' + treeNode.Level + '</div>';
            editStr += '<div class="diy">';
            editStr += '<a class="layui-btn layui-btn-xs modify" title="修改" data-id='+treeNode.id+' >修改</a>';
            editStr += '<a class="layui-btn layui-btn-xs delate" title="删除" data-id='+treeNode.id+' >删除</a></div>';
            aObj.append(editStr);
        }
        $("body").on("click",".modify",function(){
            var id = $(this).attr("data-id")
            location.href='/tree/update-tree?id='+id;
        })
        $("body").on("click",".delate",function(){
            var id = $(this).attr("data-id")
            $.ajax({
                url:"/tree/delete-tree",
                type:"post",
                data:{id:id},
                dataType:"json",
                success:function(data){
                    if(data.code==200){
                        Utils.layer.msg(data.msg);
                        setTimeout(function(){
                            location.reload();
                        },1200)
                    }else{
                        Utils.layer.msg(data.msg)
                    }
                }
            })
        })

        function query() {
            var list=[];
            $.ajax({
                url:"/tree/index",
                type:'get',
                async:false,
                dataType:"json",
                success:function(res){
                    list=res.data;
                }
            })
            var data=[];
            $.each(list,function(index,i){
                var obj={"name":i.tree_name,"Level":i.level,"id":i.id,"pId":i.pid}
                data.push(obj)
            })
            
            //初始化列表
            zTreeNodes = data;
            //初始化树
            $.fn.zTree.init($("#dataTree"), setting, zTreeNodes);
            //添加表头
            var li_head = ' <li class="head"><a><div class="diy">分类名称</div>' +
                '<div class="diy">层级</div><div class="diy">操作</div></a></li>';
            var rows = $("#dataTree").find('li');
            if (rows.length > 0) {
                rows.eq(0).before(li_head)
            } else {
                $("#dataTree").append(li_head);
                $("#dataTree").append('<li ><div style="text-align: center;line-height: 30px;" >无符合条件数据</div></li>')
            }
        }
        // excel导入
        $('#tree-excel').on('change',function (event) {
            var fileList = event.target.files;
            if(!fileList || fileList.length == 0){
                return;
            }
            var formData = new FormData();
            for(var i=0,len=fileList.length;i<len;i++){
                formData.append('excel',fileList[i]);
            }
            $.ajax({
                'type':'POST',
                'url':'/tree/import-menu',
                'data':formData,
                'dataType':'json',
                'contentType': false,
                'cache': false,
                'processData': false,
                'success':function(data){
                    layer.closeAll();
                    layer.alert(data.msg);
                }
            });
        })

    })
}